﻿using System;
using System.Configuration;

namespace Triad.Controls.Web.Configuration
{
    /// <summary>
    /// Configuration for caching
    /// </summary>
    public class CachingSection : ConfigurationSection
    {
        [ConfigurationProperty("CachingTimeSpan", IsRequired = true)]
        public TimeSpan CachingTimeSpan
        {
            get { return (TimeSpan)base["CachingTimeSpan"]; }
            set { base["CachingTimeSpan"] = value; }
        }

        [ConfigurationProperty("FileExtensions", IsDefaultCollection = true, IsRequired = true)]
        public FileExtensionCollection FileExtensions
        {
            get { return ((FileExtensionCollection)base["FileExtensions"]); }
        }
    }

    /// <summary>
    /// List of available file extensions
    /// </summary>
    public class FileExtensionCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="tfImageCaching.Web.Configuration.FileExtension"/> at the specified index.
        /// </summary>
        /// <value></value>
        /// <remarks>
        /// Author: tim.fischer,   6/30/2008 - 9:12 AM
        /// Modified By: 
        /// </remarks>
        public FileExtension this[int index]
        {
            get { return (FileExtension)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="tfImageCaching.Web.Configuration.FileExtension"/> with the specified extension.
        /// </summary>
        /// <value></value>
        /// <remarks>
        /// Author: tim.fischer,   6/30/2008 - 9:12 AM
        /// Modified By: 
        /// </remarks>
        public new FileExtension this[string extension]
        {
            get { return (FileExtension)BaseGet(extension); }
            set
            {
                if (BaseGet(extension) != null)
                {
                    BaseRemove(extension);
                }
                BaseAdd(value);
            }
        }

        /// <summary>
        /// Adds the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <remarks>
        /// Author: tim.fischer,   6/30/2008 - 9:12 AM
        /// Modified By: 
        /// </remarks>
        public void Add(FileExtension element)
        {
            BaseAdd(element);
        }

        /// <summary>
        /// Clears this instance.
        /// </summary>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 2:44 PM
        /// Modified By: 
        /// </remarks>
        public void Clear()
        {
            BaseClear();
        }

        /// <summary>
        /// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </summary>
        /// <returns>
        /// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </returns>
        /// <remarks>
        /// Author: tim.fischer,   6/30/2008 - 9:12 AM
        /// Modified By: 
        /// </remarks>
        protected override ConfigurationElement CreateNewElement()
        {
            return new FileExtension();
        }

        /// <summary>
        /// Gets the element key for a specified configuration element when overridden in a derived class.
        /// </summary>
        /// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
        /// <returns>
        /// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </returns>
        /// <remarks>
        /// Author: tim.fischer,   6/30/2008 - 9:12 AM
        /// Modified By: 
        /// </remarks>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FileExtension)element).Extension;
        }

        /// <summary>
        /// Removes the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <remarks>
        /// Author: tim.fischer,   6/30/2008 - 9:12 AM
        /// Modified By: 
        /// </remarks>
        public void Remove(FileExtension element)
        {
            BaseRemove(element.Extension);
        }

        /// <summary>
        /// Removes the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 2:44 PM
        /// Modified By: 
        /// </remarks>
        public void Remove(string name)
        {
            BaseRemove(name);
        }

        /// <summary>
        /// Removes at.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 2:44 PM
        /// Modified By: 
        /// </remarks>
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
    }

    /// <summary>
    /// Configuration for a file extension
    /// </summary>
    public class FileExtension : ConfigurationElement
    {
        [ConfigurationProperty("Extension", IsRequired = true)]
        public string Extension
        {
            get { return (string)base["Extension"]; }
            set { base["Extension"] = value.Replace(".", ""); }
        }

        [ConfigurationProperty("ContentType", IsRequired = true)]
        public string ContentType
        {
            get { return (string)base["ContentType"]; }
            set { base["ContentType"] = value; }
        }
    }
}

